package com.vlin.device.value.converter.defaut;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.vlin.device.value.converter.Condition;
import com.vlin.device.value.converter.ConditionManager;

public class DefaultConditionManager implements ConditionManager {
	
	private static final String CATEGORY_FORMAT = "%s:%s";
	
	private Map<String, Condition> categoryConverter = new ConcurrentHashMap<>();
	
	private Map<String, Condition> commonConverter = new ConcurrentHashMap<>();

	@Override
	public void setConverter(String category, String key, Condition condition) {
		
		if(category == null || key == null  || condition == null ) {
			return;
		}
		
		this.categoryConverter.put(String.format(CATEGORY_FORMAT, category,key),  condition);
		this.commonConverter.put(key, condition);
		
	}

	@Override
	public Condition getCondition(String category, String key) {
		
		if(category == null  || key == null) {
			return null;
		}
		
		return this.categoryConverter.get(String.format(CATEGORY_FORMAT, category, key));
	}

	@Override
	public Condition getCondition(String key) {
		
		if(key == null) {
			return null;
		}
		
		return this.commonConverter.get(key);
	}
	


}
